package game.module.ddz.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.ddz.bean.DdzRoom;
import game.module.ddz.logic.DdzDataManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageDdz.C2SJoinRoomCheck;
import lua.LuaMessageDdz.S2CJoinRoomCheck;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SJoinRoomCheck.id, accessLimit = 500)
public class DdzJoinRoomCheckProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(DdzJoinRoomCheckProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SJoinRoomCheck reqMsg = C2SJoinRoomCheck.parse(request);
		String room_id = reqMsg.room_id;
		int playerId = hero.getId();
		logger.info("ddz join room check,playeyId={},room_id={}", playerId, room_id);
		// 是否在房间中
		boolean isInRoom = DdzDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fang not exist
		if (!DdzDataManager.getInstance().checkRoomExist(room_id)) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		final DdzRoom ddzRoom = DdzDataManager.getInstance().getDdzRoom(room_id);
		// 人数已满
		if (ddzRoom.getPlayerList().size() >= 3) {
			S2ErrorCode respMsg = new S2ErrorCode(105);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// ret
		S2CJoinRoomCheck respMsg = new S2CJoinRoomCheck();
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
